package login;

import io.ClientSession;
import io.OperationResponse;

import java.io.IOException;
import java.util.HashMap;






import org.apache.http.client.ClientProtocolException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

public class KYLogin {
	protected static final Logger log = LoggerFactory.getLogger(KYLogin.class);

	static String serverUrl = "http://f_signin.bppstore.com/loginCheck.php";
	static String AppKey = "1d2cf8a26d7d3b8014b31305e465ee2b";

	public static void Login(OperationResponse operationRequest,
			ClientSession session) throws ClientProtocolException, IOException,
			JSONException {
		if (operationRequest.Parameters.size() < 2) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, null, ReturnCode.No, "");
			session.SendOperationResponse(response);
		}
		// String id="2454026_5519vok1mmgl8j0d03h6l0idj6";
		String id = (String) operationRequest.Parameters
				.get(ParameterType.UserID);
		String sessionid = (String) operationRequest.Parameters
				.get(ParameterType.UserPwd);
		String deviceID = (String) operationRequest.Parameters
				.get(ParameterType.DeviceID);
		String isGetIP = (String) operationRequest.Parameters
				.get(ParameterType.IsGetIP);
		String PhoneType=(String) operationRequest.Parameters
				.get(ParameterType.PhoneType);
		if (id.equals("zealm")) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, AppConfig.dicAppInfo,
					ReturnCode.GetServer, "");
			session.SendOperationResponse(response);
			return;

		}

		String sign = Util
				.getMD5Str(AppKey + id);
		String body = "tokenKey=" + id + "&" + "sign=" + sign;
		body = new String(body.getBytes("gbk"), "utf-8");
		// HttpsUtil.post(serverUrl, body);
		String result = Util.sendPost(serverUrl, body);
		if(!result.equals("")){
		String guid = "";
		try {
			JSONObject token = new JSONObject(result);
			String code = token.optString("code");
			if (code.equals("0")) {
				JSONObject data = token.getJSONObject("data");
				guid = data.optString("guid");
				guid = "KY" + guid;
			} else {
				return;
			}
		
		} catch (Exception e) {
			log.error("ky return have a expection ="+e.toString());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		operationRequest.Parameters.put(ParameterType.UserID, guid);
		HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
		myPrams.put(ParameterType.UserID, guid);
		OperationResponse responseID = new OperationResponse(
				operationRequest.OperationCode, myPrams, ReturnCode.HasID, "");
		session.SendOperationResponse(responseID);
		Util.sdkReceived(operationRequest, session, guid, deviceID, isGetIP,"KY",PhoneType,"");
		}else{
			log.info("ky return have a error result ="+result);
		}
	}
	public  Runnable getLoginKY(OperationResponse operationResponse, ClientSession session){ 
		class LoginKY implements Runnable { 
			private OperationResponse t_operationResponse;
			private ClientSession t_session;
			public LoginKY(OperationResponse operationResponse, ClientSession session)
			{
				t_operationResponse = operationResponse;
				t_session = session;
			}
			
			@Override
			public void run() {
					try {
						Login(t_operationResponse, t_session);
					} catch (Exception e) {
						log.info(Thread.currentThread().getName() + " Login task Error!!!");
					}
				}
        }
	    return new LoginKY(operationResponse, session); 
	 } 
}
